package org.snowin.common;

import java.io.File;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.snowin.utils.GlobalInfo;


public class InitialEnvironment {
    SessionData sessionData = new SessionData();
    Document doc = null;
    List<String> emails = new ArrayList();
    HashMap<String, String> webDriverhash = new HashMap();
    HashMap<String, String> mailSender = new HashMap();


    public InitialEnvironment() {

        SAXReader saxReader = new SAXReader();
        try {
            String sp = System.getProperty(GlobalInfo.FILE_SEPARATOR);
            this.doc = saxReader.read(new File(GlobalInfo.getRootPath() + sp + "config" + sp + "conf" + sp + "wtd.cfg.xml"));

            System.out.println(doc);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        setPcHost();
        setShareFolder();
        setHostNameToSession(this.doc);

        setWebDrivers(this.doc);

        setMailReceptors(this.doc);

        setOnlineVarible();
    }

    public List<String> getMailReceptors() {
        return this.emails;
    }

    public void setMailReceptors(Document doc) {
        List<Element> emailElements = doc.selectNodes("//MailReceptor/user");
        for (int i = 0; i < emailElements.size(); i++) {
            this.emails.add(((Element) emailElements.get(i)).attributeValue("email"));
        }
    }

    public HashMap<String, String> getMailSender() {
        boolean enableStr = true;
        boolean useDefault = true;
        try {
            Element urlElement = (Element) this.doc.selectSingleNode("//MailSender");
            enableStr = !urlElement.attributeValue("enable").equalsIgnoreCase("false");
            useDefault = !urlElement.attributeValue("usedefault").equalsIgnoreCase("false");
        } catch (Exception localException) {
        }
        if (enableStr) {
            this.mailSender.put("isEnable", "true");
            this.mailSender.put("useDefault", useDefault ? "true" : "false");

            Element protocolEl = (Element) this.doc.selectSingleNode("//MailSender/protocol");

            Element hostEl = (Element) this.doc.selectSingleNode("//MailSender/host");

            Element portEl = (Element) this.doc.selectSingleNode("//MailSender/port");

            Element mailEl = (Element) this.doc.selectSingleNode("//MailSender/mail");

            Element passwordEl = (Element) this.doc.selectSingleNode("//MailSender/password");

            this.mailSender.put("protocol", protocolEl.attributeValue("value"));
            this.mailSender.put("host", hostEl.attributeValue("value"));
            this.mailSender.put("port", portEl.attributeValue("value"));
            this.mailSender.put("user", mailEl.attributeValue("value"));
            this.mailSender.put("password", passwordEl.attributeValue("value"));
        } else {
            this.mailSender.put("isEnable", "false");
        }
        return this.mailSender;
    }

    public String getProxy() {
        String proxy = "";
        try {
            Element urlElement = (Element) this.doc.selectSingleNode("//Proxy");
            proxy = urlElement.attributeValue("value");
        } catch (Exception localException) {
        }
        return proxy;
    }

    public String getMailSubject() {
        String mailSubject = "Automation Test Report";
        try {
            Element urlElement = (Element) this.doc.selectSingleNode("//MailSubject");
            mailSubject = urlElement.attributeValue("value");
        } catch (Exception localException) {
        }
        return mailSubject;
    }

    public HashMap<String, String> getWebDrivers() {
        return this.webDriverhash;
    }

    public void setWebDrivers(Document doc) {
        List<Element> webDriveEles = doc.selectNodes("//Browser/*");
        for (Element e : webDriveEles) {
            String location = e.attributeValue("location");
            String browserName = e.getName();
            this.webDriverhash.put(browserName, location);
        }
    }

    public SessionData getSessionData() {
        return this.sessionData;
    }

    public void setHostName(String hostName) {
        this.sessionData.setHostName(hostName);
    }

    public void setHostNameToSession(Document doc) {
        String hostName = "";
        try
        {
            Element urlElement = (Element)doc.selectSingleNode("//host-url/url");
            hostName = urlElement.attributeValue("name");
        }
        catch (Exception localException) {}
        this.sessionData.setHostName(hostName);

    }

    public void setPcHost() {
        try {
            InetAddress a = InetAddress.getLocalHost();
            GlobalInfo.pcHostName = a.getHostName();
            GlobalInfo.pcIP = a.getHostAddress().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setShareFolder() {
        try {
            String sp = System.getProperty(GlobalInfo.FILE_SEPARATOR);
            if (GlobalInfo.os.contains("win")) {
                Runtime.getRuntime().exec("net share " + GlobalInfo.pcHostName + "=" + GlobalInfo.rootPath + sp + "bin" + sp + "logs");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getPcHost() {
        return GlobalInfo.pcHostName;
    }

    public String getPcIp() {
        return GlobalInfo.pcIP;
    }

    private void setOnlineVarible() {
        GlobalInfo.isOnlineTest = this.sessionData.getHostName().contains("onlinehost.com");
    }
}
